﻿Imports Catalyst.Web.DynamicData

Partial Public Class ForeignKey
    Inherits Catalyst.Web.DynamicData.EqualsFilterUserControlBase

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        PopulateListControl(DropDownList1)
    End Sub

    Public Overrides Property Value() As String
        Get
            Return DropDownList1.SelectedValue
        End Get
        Set(ByVal value As String)
            DropDownList1.SelectedIndex = -1
            Dim li As New ListItem
            li = DropDownList1.Items.FindByValue(value)
            If li IsNot Nothing Then
                li.Selected = True
            End If
        End Set
    End Property

    Public Overrides ReadOnly Property ValueType() As System.TypeCode
        Get
            Return ForeignKeyColumn.ParentTable.PrimaryKeyColumns(0).TypeCode
        End Get
    End Property

    Public Overrides Function GetWhereParameters(ByVal dataSource As System.Web.DynamicData.IDynamicDataSource) As System.Collections.Generic.IEnumerable(Of System.Web.UI.WebControls.Parameter)

        Dim param As New EqualsExpressionParameter()
        With param
            .Name = String.Format("{0}.{1}", Column.Name, ForeignKeyColumn.ParentTable.PrimaryKeyColumns(0).Name)
            .ValueType = ValueType
            .Value = Value
        End With

        Return New Parameter() {param}

    End Function


End Class